﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace IOCPServer
{
    class SocketAsyncEventArgsPool
    {
        private readonly Stack<SocketAsyncEventArgs> m_pool;
        private readonly object syncLocker = new object();

        public SocketAsyncEventArgsPool(int capacity)
        {
            m_pool = new Stack<SocketAsyncEventArgs>(capacity);
        }

        public void Push(SocketAsyncEventArgs item)
        {
            if (item == null) { throw new ArgumentNullException("Items added to a SocketAsyncEventArgsPool cannot be null"); }
            lock (syncLocker)
            {
                if (item.AcceptSocket != null)
                    item.AcceptSocket = null;
                m_pool.Push(item);
            }
        }

        public SocketAsyncEventArgs Pop()
        {
            lock (syncLocker)
                return m_pool.Pop();
        }

        public int Count { get { return m_pool.Count; } }
    }
}
